/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.container;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.activequant.util.tools.ArrayUtils;
import org.apache.log4j.Logger;


/**
 * An ITradeSystemContainer implementation. Holds the following associated variables:
 * <ul>
 * <li>systemsContexts(List&lt;ITradeSystemContext&gt;)</li>
 * <li>systemLoaders(ITradeSystemFactory[])</li>
 * <li>isRunning(boolean)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [11.05.2007] Created (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 */
public class SimpleClassPathContainer implements ITradeSystemContainer {
	
	private final static Logger log = Logger.getLogger(SimpleClassPathContainer.class);
	/**
	 * private List&lt;ITradeSystemContext&gt; systemsContexts = new ArrayList&lt;ITradeSystemContext&gt;();
	 */
	private List<ITradeSystemContext> systemsContexts = new ArrayList<ITradeSystemContext>();
	/**
	 * private ITradeSystemFactory[] systemLoaders = {};
	 */
	private ITradeSystemFactory[] systemLoaders = {/* new SpringContextFactory() */};
	/**
	 * private boolean isRunning = false;
	 */
	private boolean isRunning = false;
	/**
	 * constructs a SimpleClassPathContainer(ITradeSystemContainer implementation) using the given tradesystems(String...) paths to load/initialize its
	 * associated systemsContexts(List&lt;ITradeSystemContext&gt;)
	 * @param tradesystems
	 * @throws Exception
	 */
	public SimpleClassPathContainer(String... tradesystems) throws Exception {
		for(String tradesystem: tradesystems) {
			ITradeSystemContext context = loadTradeSystem(new File(tradesystem));
			systemsContexts.add(context);
		}
	}
	/**
	 * invokes <code>context.start()</code> on each context in the associated systemsContexts(List&lt;ITradeSystemContext&gt;) and then sets the associated
	 * isRunning(boolean) to true
	 */
	public void start() throws Exception {
		log.info("Starting " + this.getClass().getSimpleName());
		for(ITradeSystemContext context : systemsContexts){
			context.start();
		}
		isRunning = true;
	}
	/**
	 * invokes <code>context.stop()</code> on each context in the associated systemsContexts(List&lt;ITradeSystemContext&gt;) and then sets the associated
	 * isRunning(boolean) to false
	 */
	public void stop() throws Exception {
		log.info("Stopping" + this.getClass().getSimpleName());
		for(ITradeSystemContext context : systemsContexts) {
			context.stop();
		}
		isRunning = false;
	}
	
	/**
	 * returns the associated isRunning(boolean)
	 */
	public boolean isRunning() {
		return isRunning;
	}
	/**
	 * loops through the associated systemLoaders(ITradeSystemFactory[]) looking for one that supports the given tradesystem(File). Once it finds one,
	 * it invokes <code>loader.loadContext(tradesystem)</code> and returns the resulting ITradeSystemContext
	 * @param tradesystem
	 * @return
	 * @throws Exception
	 */
	private ITradeSystemContext loadTradeSystem(File tradesystem) throws Exception {
		for(ITradeSystemFactory loader: systemLoaders) {
			if(loader.isSupported(tradesystem)) {
				return loader.loadContext(tradesystem);
			}
		}
		throw new Exception("No loader found for file '"+tradesystem+"'.");
	}
	/**
	 * returns the associated systemsContexts(List&lt;ITradeSystemContext&gt;) as an Array
	 * @return
	 */
	public ITradeSystemContext[] getSystemsContexts() {
		return systemsContexts.toArray(new ITradeSystemContext[] {});
	}
	/**
	 * sets the associated systemsContexts(List&lt;ITradeSystemContext&gt;) with the given systemsContexts(ITradeSystemContext...)
	 * @param systemsContexts
	 */
	public void setSystemsContexts(ITradeSystemContext... systemsContexts) {
		this.systemsContexts = ArrayUtils.asList(systemsContexts);
	}
}
